package sg.edu.nus.iss.awsdynamodb;

import sg.edu.nus.iss.CONFIG;
import sg.edu.nus.iss.stockex.LinearRegression;
import sg.edu.nus.iss.stockex.MovingAverage;
import sg.edu.nus.iss.stockex.RelativeStrengthIndex;
import sg.edu.nus.iss.stockex.StochasticOscillator;
import sg.edu.nus.iss.stockex.StockCode;
import sg.edu.nus.iss.stockex.StockQuote;

import java.io.*;


public class DynamoDBTableExec {
	public static void run()
	{
		DynamoDB.createTable(CONFIG.STOCK_CODE_TABLE_NAME, CONFIG.STOCK_CODE_PRIMARY_KEY, CONFIG.STOCK_CODE_PRIMARY_KEY_TYPE,CONFIG.STOCK_CODE_RANGE_KEY,CONFIG.STOCK_CODE_RANGE_KEY_TYPE);
		DynamoDB.createTable(CONFIG.STOCK_QUOTE_TABLE_NAME, CONFIG.STOCK_QUOTE_PRIMARY_KEY, CONFIG.STOCK_QUOTE_PRIMARY_KEY_TYPE,CONFIG.STOCK_QUOTE_RANGE_KEY,CONFIG.STOCK_QUOTE_RANGE_KEY_TYPE);
		DynamoDB.createTable(CONFIG.LINEAR_REGRESSION_TABLE_NAME, CONFIG.LINEAR_REGRESSION_PRIMARY_KEY, CONFIG.LINEAR_REGRESSION_PRIMARY_KEY_TYPE,CONFIG.LINEAR_REGRESSION_RANGE_KEY,CONFIG.LINEAR_REGRESSION_PRIMARY_KEY_TYPE);
		DynamoDB.createTable(CONFIG.STOCHASTIC_OSCILLATOR_TABLE_NAME, CONFIG.STOCHASTIC_OSCILLATOR_PRIMARY_KEY, CONFIG.STOCHASTIC_OSCILLATOR_PRIMARY_KEY_TYPE,CONFIG.STOCHASTIC_OSCILLATOR_RANGE_KEY,CONFIG.STOCHASTIC_OSCILLATOR_RANGE_KEY_TYPE);
		DynamoDB.createTable(CONFIG.MOVING_AVG_TABLE_NAME, CONFIG.MOVING_AVG_PRIMARY_KEY, CONFIG.MOVING_AVG_PRIMARY_KEY_TYPE,CONFIG.MOVING_AVG_RANGE_KEY,CONFIG.MOVING_AVG_RANGE_KEY_TYPE);
		DynamoDB.createTable(CONFIG.RELATIVE_STRENGHT_INDEX_TABLE_NAME, CONFIG.RELATIVE_STRENGHT_INDEX_PRIMARY_KEY, CONFIG.RELATIVE_STRENGHT_INDEX_PRIMARY_KEY_TYPE,CONFIG.RELATIVE_STRENGHT_INDEX_RANGE_KEY,CONFIG.RELATIVE_STRENGHT_INDEX_RANGE_KEY_TYPE);
	
		DynamoDB.waitForTableToBecomeAvailable(CONFIG.STOCK_CODE_TABLE_NAME);
		DynamoDB.waitForTableToBecomeAvailable(CONFIG.STOCK_QUOTE_TABLE_NAME);
		DynamoDB.waitForTableToBecomeAvailable(CONFIG.LINEAR_REGRESSION_TABLE_NAME);
		DynamoDB.waitForTableToBecomeAvailable(CONFIG.STOCHASTIC_OSCILLATOR_TABLE_NAME);
		DynamoDB.waitForTableToBecomeAvailable(CONFIG.MOVING_AVG_TABLE_NAME);
		DynamoDB.waitForTableToBecomeAvailable(CONFIG.RELATIVE_STRENGHT_INDEX_TABLE_NAME);
		
		StockCodeTable stockCodeTable = new StockCodeTable();
		LinearRegressionTable linearRegressionTable = new LinearRegressionTable();
		StochasticOscillatorTable stochasticOscillatorTable = new StochasticOscillatorTable();
		MovingAverageTable movingAverageTable = new MovingAverageTable();
		RelativeStrengthIndexTable relativeStrengthIndexTable = new RelativeStrengthIndexTable();
		StockQuoteTable stockQuoteTable = new StockQuoteTable();
		FileInputStream fatstream = null;
		

		

		
		

		//Stock Codes Data Input
		try {
			
			fatstream = new FileInputStream(CONFIG.STOCK_CODE_FILENAME);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		DataInputStream in = new DataInputStream(fatstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;
		
		System.out.println("file open");
		in = new DataInputStream(fatstream);
		br = new BufferedReader(new InputStreamReader(in));
		strLine = null;

		StockCode stockCode = new StockCode();
		try {
			while ((strLine = br.readLine()) != null){
				System.out.println (strLine);
				stockCode = StockCode.parseString(strLine);
				stockCodeTable.Add(stockCode);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		
		
		//Linear Regression Data Input
		try {
			fatstream = new FileInputStream(CONFIG.LINEAR_REGRESSION_FILENAME);
			
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		System.out.println("file open");

		LinearRegression linearRegression;
		
		try {
			while ((strLine = br.readLine()) != null){
				System.out.println (strLine);
				linearRegression = new LinearRegression(strLine);
				linearRegressionTable.Add(linearRegression);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		//Stock Quote Data Input
		File folder = new File(CONFIG.STOCK_QUOTE_FOLDER);
		File[] listOfFiles = folder.listFiles();
		String files;
		StockQuote stockQuote;
		
		for (int i=0; i< listOfFiles.length; i++){
			if (listOfFiles[i].isFile()){
				files = listOfFiles[i].getName();
				
				try {
					fatstream = new FileInputStream(CONFIG.STOCK_QUOTE_FOLDER+"/"+files);
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				System.out.println("file open");
				in = new DataInputStream(fatstream);
				br = new BufferedReader(new InputStreamReader(in));
				strLine = null;

				
				try {
					while ((strLine = br.readLine()) != null){
						System.out.println (strLine);
						stockQuote = StockQuote.parseString(strLine);
						stockQuoteTable.Add(stockQuote);
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		
		
		
		//Stochastic Oscillator Data Input
		try {
			
			fatstream = new FileInputStream(CONFIG.STOCHASTIC_OSCILLATOR_FILENAME);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("file open");
		in = new DataInputStream(fatstream);
		br = new BufferedReader(new InputStreamReader(in));
		strLine = null;

		StochasticOscillator stochasticOscillator;
		try {
			while ((strLine = br.readLine()) != null){
				System.out.println (strLine);
				stochasticOscillator = new StochasticOscillator(strLine);
				stochasticOscillatorTable.Add(stochasticOscillator);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		//Moving Average Data Input
		try {
			
			fatstream = new FileInputStream(CONFIG.MOVING_AVERAGE_FILENAME);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("file open");
		in = new DataInputStream(fatstream);
		br = new BufferedReader(new InputStreamReader(in));
		strLine = null;

		MovingAverage movingAverage;
		try {
			while ((strLine = br.readLine()) != null){
				System.out.println (strLine);
				movingAverage = new MovingAverage(strLine);
				movingAverageTable.Add(movingAverage);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//Relative Strength Index Data Input
		try {
			
			fatstream = new FileInputStream(CONFIG.RELATIVE_STRENGTH_INDEX_FILENAME);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("file open");
		in = new DataInputStream(fatstream);
		br = new BufferedReader(new InputStreamReader(in));
		strLine = null;

		RelativeStrengthIndex relativeStrengthIndex;
		try {
			while ((strLine = br.readLine()) != null){
				System.out.println (strLine);
				relativeStrengthIndex = new RelativeStrengthIndex(strLine);
				relativeStrengthIndexTable.Add(relativeStrengthIndex);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
	
	public static void destroy()
	{
		DynamoDB.deleteTable(CONFIG.STOCK_QUOTE_TABLE_NAME);
		DynamoDB.deleteTable(CONFIG.LINEAR_REGRESSION_TABLE_NAME);
		DynamoDB.deleteTable(CONFIG.STOCHASTIC_OSCILLATOR_TABLE_NAME);
		DynamoDB.deleteTable(CONFIG.MOVING_AVG_TABLE_NAME);
		DynamoDB.deleteTable(CONFIG.RELATIVE_STRENGHT_INDEX_TABLE_NAME);
		
	}
}
